package ema.tpjee.banking.controler;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import ema.tpjee.banking.dao.BankDao;
//import ema.tpjee.banking.dao.BankDaoImpl;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.Customer;
import ema.tpjee.banking.session.Session;




/**
 * Simple client of the stateless.
 * 
 * @author pfister
 */


/**
 * le contexte n'est pas maintenu sur le serveur ejb par un statefull bean, 
 * mais sur le web tiers par un backing bean jouant le r�le de business delegate et de contr�leur (scope session)
 */
public final class BankControler implements Serializable {

	private static final long serialVersionUID = -861746215317587187L;

	private BankDao bankDao;

	private  Session session;

	public List<Bank> getBanks() {
		return bankDao.getList();
	}

	public void setBanks(List<Bank> banks) {
		//this.banks = banks;
	}

	private Bank getCurrentBank() {
		return session.getCurrentBank();
	}

	private void setCurrentBank(Bank currentBank) {
		session.setCurrentBank(currentBank);
	}

	public String add() {
		setCurrentBank(bankDao.add(getCurrentBank()));
		return "list";
	}

	public String find(int id) {
		setCurrentBank(bankDao.find(id));
		return "edit";
	}

	public String delete() {
		setCurrentBank(bankDao.delete(getCurrentBank()));
		return "list";
	}

	public String first() {
		setCurrentBank(bankDao.first());
		return "edit";
	}

	public String last() {
		setCurrentBank(bankDao.last());
		return "edit";
	}

	public String prior() {
		setCurrentBank(bankDao.prior(getCurrentBank()));
		return "edit";
	}

	public String next() {
		setCurrentBank(bankDao.next(getCurrentBank()));
		return "edit";
	}

	
	public String edit() {
		System.out.println(getCurrentBank().getId());
		return "edit";
	}
	
	
	public String clone() {
		setCurrentBank(bankDao.clone(getCurrentBank()));
		return "edit";
	}
	
	public String showCustomers() {
		System.out.println("action "+this.getClass().getName()+" showCustomers()");
		return "customers";
	}


	public String create() {
		setCurrentBank(bankDao.create());
		return "edit";
	}

	public String save() {
		setCurrentBank(bankDao.merge(getCurrentBank()));
		return "list";
	}
	
	public String cancel() {
		// do nothing setCurrentBank_(bankDao.merge(getCurrentBank_()));
		System.out.println("cancel "+getCurrentBank().toString());
		return "list";
	}	

	public String foobar() {
		setCurrentBank(bankDao.foobar(getCurrentBank()));
		return "edit";
	}




	public String getDummy() {
		return test();
	}

	public void setDummy(String s) {

	}

	public String test() {
		String result = "";
		List<Bank> banks = getBanks();
		if (banks != null) {
			for (Bank bank : banks) {
				result += "Liste des clients pour la banque " + bank.getName()
						+ " :\n";
				Collection<Customer> customers = bank.getCustomers();
				if (customers == null) {
					result += " Aucun client\n";
				} else {
					for (Customer customer : customers)
						result += "    client: " + customer.getName() + "\n";
				}
			}
		} else {
			result += "Aucune banque\n";
		}
		return result;
	}

	
	
	
	public void createLocalDao() {
		System.out.println("creating LocalDao");
		//bankDao = new BankDaoImpl();
	}
	
	public void createEjbDao() {
		System.out.println("creating EjbDao");
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");
		try {
			bankDao = (BankDao) new InitialContext()
					.lookup("BankDaoImpl/remote");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	
	
	public BankControler() {
		System.out.println("creating BankControler");
	    createEjbDao(); 
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public BankDao getBankDao() {
		return bankDao;
	}

}
